package com.example.demotest.service;

import com.example.demotest.pojo.Complaint;
import com.example.demotest.pojo.Product;
import com.example.demotest.pojo.User;
import com.example.demotest.pojo.dto.ComplaintDto;
import com.example.demotest.pojo.dto.ComplaintUpdateDto;
import com.example.demotest.repository.ComplaintRepository;
import com.example.demotest.repository.ProductRepository;
import com.example.demotest.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ComplaintService implements IComplaintService {

    @Autowired
    private ComplaintRepository complaintRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Complaint submitComplaint(Integer userId, ComplaintDto complaintDto) {
        User complainant = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("找不到投诉人"));


        Product product = null;
        if (complaintDto.getProductId() != null) {
            product = productRepository.findById(complaintDto.getProductId())
                    .orElseThrow(() -> new RuntimeException("找不到商品"));
        }

        User defendant = null;
        if (complaintDto.getDefendantId() != null) {
            defendant = userRepository.findById(complaintDto.getDefendantId())
                    .orElseThrow(() -> new RuntimeException("找不到被投诉人"));
        }
        else {
            if (product != null) {
                defendant = userRepository.findById(product.getUserId())
                        .orElseThrow(() -> new RuntimeException("该商品没有拥有者"));
            }
            else {
                throw new IllegalArgumentException("没有投诉对象");
            }
        }

        Complaint complaint = new Complaint();
        complaint.setComplainant(complainant);
        complaint.setDefendant(defendant);
        complaint.setReportedProduct(product);
        complaint.setContent(complaintDto.getContent());
        complaint.setCreatedAt(LocalDateTime.now());

        return complaintRepository.save(complaint);
    }

    /*
     * 获取某个用户的全部投诉信息
     */
    @Override
    public List<Complaint> getComplaintsByComplainant(Integer userId) {
        return complaintRepository.findByComplainant(userId);
    }

    /*
     * 获取某个用户的全部被投诉信息
     */
    @Override
    public List<Complaint> getComplaintsByDefendant(Integer userId) {
        return complaintRepository.findByDefendant(userId);
    }

    /*
     * 获取某个商品的全部投诉信息
     */
    @Override
    public List<Complaint> getComplaintsByProduct(Integer productId) {
        Product reportedProduct = productRepository.findById(productId)
                .orElseThrow(() -> new RuntimeException("投诉商品未知"));

        return complaintRepository.findByReportedProduct(reportedProduct);
    }

    /*
     * 获取所有投诉信息
     */
    @Override
    public List<Complaint> getAllComplaints() {
        return complaintRepository.findAll();
    }

    /*
     * 通过投诉id获取投诉
     */
    @Override
    public Complaint getComplaintsByComplaint(Integer complaintId){
        Complaint complaint = complaintRepository.findById(complaintId)
                .orElseThrow(() -> new RuntimeException("未找到该条投诉"));
        return complaint;
    }
    /*
     * 管理员处理投诉
     */
    @Override
    public void adminUpdateComplaint(ComplaintUpdateDto complaintUpdateDto) {
        Complaint complaint = complaintRepository.findById(complaintUpdateDto.getComplaintId())
                .orElseThrow(() -> new RuntimeException("投诉记录未找到"));

        if (complaintUpdateDto.getStatus() != null) {
            complaint.setStatus(complaintUpdateDto.getStatus());
        }

        if (complaintUpdateDto.getAdminRemark() != null) {
            complaint.setAdminRemark(complaintUpdateDto.getAdminRemark());
        }

        complaintRepository.save(complaint);
    }


}
